<template>
    <div>
        <el-form ref="form" status-icon :rules="form_rules" :model="form" label-width="100px">
            <el-form-item label="角色名称" prop="role_name">
                <el-input v-model="form.role_name"></el-input>
            </el-form-item>
            <el-form-item label="角色备注">
                <el-input type="textarea" v-model="form.role_desc" :rows="3"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit()">保存</el-button>
                <el-button @click="closeDialog()">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data () {
        return {
            form: {
                role_name: '',
                role_desc: ''
            },
            form_rules: {
                role_name: [
                    { required: true, message: '请输入角色名称', trigger: 'blur' }
                ]
            }
        }
    },
    props: {
        id: {
            type: Number
        }
    },
    created() {
        if (this.id) { // 编辑
            this.getOldData();
        }
    },
    methods: {
        getOldData() { // 获取详情数据

            this.$https.get('/api/company_role/get_company_roledetails?role_id=' + this.id).then((result) => {
                if (result.data.code == 0) {
                    let _data = result.data.data;
                    this.form.role_name = _data.role_name;
                    this.form.role_desc = _data.role_desc;
                } else {
                    this.$message({
                        type: 'error',
                        showClose: true,
                        message: result.data.message
                    })
                }
            })
        },
        submit() { // 提交
            this.$refs.form.validate((valid) => {
                if (valid) {
                    let _postData = {
                        role_name: this.form.role_name,
                        role_desc: this.form.role_desc
                    }
                    if (this.id) { // 编辑
                        _postData.role_id = parseInt(this.id);
                        this.$https.post('/api/company_role/update_company_role', _postData).then((result) => {
                            if (result.data.code == 0) {
                                this.$message({
                                    type: 'success',
                                    showClose: true,
                                    message: '编辑成功',
                                    duration: 1500,
                                    onClose: () => {
                                        // 关闭当前dialog，
                                        this.closeDialog('dialogRoleEdit');
                                        // 刷新列表
                                        this.$emit('parentGetDataList');
                                    }
                                })
                            } else {
                                this.$message({
                                    type: 'error',
                                    showClose: true,
                                    message: result.data.message
                                })
                            }
                        })
                    } else { // 新增

                        this.$https.post('/api/company_role/create_company_role', _postData).then((result) => {
                            if (result.data.code == 0) {
                                this.$message({
                                    type: 'success',
                                    showClose: true,
                                    message: '添加成功',
                                    duration: 1500,
                                    onClose: () => {
                                        // 关闭当前dialog，
                                        this.closeDialog('dialogRoleAdd');
                                        // 刷新列表
                                        this.$emit('parentGetDataList');
                                    }
                                })
                            } else {
                                this.$message({
                                    type: 'error',
                                    showClose: true,
                                    message: result.data.message
                                })
                            }
                        })
                    }
                    

                } else {
                    console.error('验证失败');
                    return false;
                }
            });
        },
        closeDialog(name) { // 关闭当前
            // this.$refs.form.resetFields(); // 重置表单
            if (!name) {
                this.$emit('closeDialog', 'dialogRoleAdd'); // 执行父组件关闭方法
                this.$emit('closeDialog', 'dialogRoleEdit'); // 执行父组件关闭方法
            } else {
                this.$emit('closeDialog', name); // 执行父组件关闭方法
            }
        }
    }
}
</script>
<style scoped>
</style>